﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevelopStudio.Basic;
using DevelopStudio.Control.Basic;

namespace DevelopStudio.Control.FormPage
{
    public partial class AssignBugForm : ChangeListFormBase
    {
        private AssignBugForm()
        {
            InitializeComponent();
        }

        public AssignBugForm(ChangelistInfo changelistInfo)
            : base(changelistInfo)
        {
            InitializeComponent();
            this.Text += " - " + this.ChangelistInfo.Name + " - " + this.ChangelistInfo.WorkingCopyPath;
            this.buttonRemove.Image = new Bitmap(Properties.Resources.Delete.ToBitmap(), new Size(16, 16));
            this.buttonAdd.Image = new Bitmap(Properties.Resources.Add.ToBitmap(), new Size(16, 16));
        }

        private void buttonRemove_Click(object sender, EventArgs e)
        {
            ListViewItem[] selected = new ListViewItem[this.listView1.SelectedItems.Count];
            this.listView1.SelectedItems.CopyTo(selected, 0);

            foreach (ListViewItem var in selected)
            {
                BugInfo bugInfo = var.Tag as BugInfo;

                if (bugInfo != null)
                {
                    bugInfo.AssignedStatus = AppEnum.BugAssignedStatus.Removed;
                }

                this.listView1.Items.Remove(var);
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            using (AddBugs dlg = new AddBugs())
            {
                if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }

                BugInfo bugInfo = dlg.BugInfo;

                if (bugInfo != null)
                {
                    foreach (ListViewItem var in this.listView1.Items)
                    {
                        BugInfo existedInfo = var.Tag as BugInfo;

                        if (existedInfo == null)
                        {
                            continue;
                        }

                        if (existedInfo.ID == bugInfo.ID)
                        {
                            MessageBox.Show(string.Format(Properties.Resources.ExistedBugsInListView, existedInfo.ID));
                            return;
                        }
                    }

                    bugInfo.AssignedStatus = AppEnum.BugAssignedStatus.NewAdded;

                    this.listView1.Items.Add(new ListViewItem(new string[] { bugInfo.ID.ToString(), bugInfo.Subject, bugInfo.Description }) { Tag = bugInfo});
                }
            }
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (this.textBoxComment.Text.Contains('[') ||
                this.textBoxComment.Text.Contains(']'))
            {
                MessageBox.Show(Properties.Resources.ErrorComment);
                this.DialogResult = System.Windows.Forms.DialogResult.None;
                return;
            }

            foreach (ListViewItem var in this.listView1.Items)
            {
                BugInfo targetInfo = var.Tag as BugInfo;

                if (targetInfo == null)
                {
                    continue;
                }

                if (targetInfo.AssignedStatus == AppEnum.BugAssignedStatus.NewAdded)
                {
                    this.ChangelistInfo.BugInfos.Add(targetInfo);
                }
            }

            foreach (BugInfo  var in this.ChangelistInfo.BugInfos)
            {
                if (var.AssignedStatus == AppEnum.BugAssignedStatus.Removed ||
                    var.AssignedStatus == AppEnum.BugAssignedStatus.NewAdded)
                {
                    var.UpdateRedmine(this.ChangelistInfo, AppEnum.ChangelistStatus.Local);
                }
            }

            this.ChangelistInfo.BugInfos.RemoveAll(o => o.AssignedStatus == AppEnum.BugAssignedStatus.Removed);
            this.ChangelistInfo.BugInfos.ForEach(o => o.AssignedStatus = AppEnum.BugAssignedStatus.Added);
            this.ChangelistInfo.Status = (int)AppEnum.ChangelistStatus.Local;
            this.ChangelistInfo.Comment = this.textBoxComment.Text;
            Utility.SaveEnvironmentSettings();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            foreach (BugInfo var in this.ChangelistInfo.BugInfos)
            {
                var.AssignedStatus = AppEnum.BugAssignedStatus.Added;
            }
        }

        private void AssignBugForm_Load(object sender, EventArgs e)
        {
            foreach (BugInfo bugInfo in this.ChangelistInfo.BugInfos)
            {
                this.listView1.Items.Add(new ListViewItem(new string[] { bugInfo.ID.ToString(), bugInfo.Subject, bugInfo.Description }) { Tag = bugInfo });
            }

            this.textBoxComment.Text = this.ChangelistInfo.Comment;
        }
    }
}
